package com.travelsky.airchina.boarding.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.travelsky.airchina.boarding.common.ConstType;
import com.travelsky.airchina.boarding.domain.Resource;
import com.travelsky.airchina.boarding.domain.User;
import com.travelsky.airchina.boarding.dto.Menu;
import com.travelsky.airchina.boarding.service.UserService;
/**
 * 登录和退出
 * @author haichenwang
 *
 */
@Controller
@SessionAttributes("adminSessionUser")
@RequestMapping(value="/system")
public class AdminLoginController {
	@Autowired
	private UserService userService;
	private PasswordEncoder encoder = new ShaPasswordEncoder();
	/**
	 * 转到登录页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String toLogin(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Object isLoginError = session.getAttribute("userNameOrpasswordIsError");
		boolean isLoginErrorFlag = false;
		if(isLoginError!=null){
			isLoginErrorFlag = (Boolean)isLoginError;
		}
		if(isLoginErrorFlag){
			request.setAttribute("userNameOrpasswordIsError", true);
			session.removeAttribute("userNameOrpasswordIsError");
		}
		return "system/login";
	}
	/**
	 * 登录操作
	 * @param loginName
	 * @param loginPassword
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public String login(@RequestParam String loginName,@RequestParam String loginPassword,ModelMap model,HttpServletRequest request) {
		User user = userService.getUser(loginName);
		String encodePassword = encoder.encodePassword(loginPassword,ConstType.SALT_PRI+loginName);
		//String encodePassword = encoder.encodePassword(loginPassword,loginName);
		HttpSession session = request.getSession();
		if(user!=null && user.getLoginPassword().equals(encodePassword)){
			//将user放入到session中
			model.put(ConstType.ADMIN_SESSION_USER, user);
			List<Resource> userOperationList = userService.findUserOperationResourceList(loginName);
			if(userOperationList!=null && userOperationList.size()>0){
				List<Long> roleColumnIdList = userService.findRoleColumnIdListList(loginName);
				//List<String> userOperationURIList = new ArrayList<String>();
				Map<String,String> userOperationURIMap = new HashMap<String,String>();
				List<Menu> menus = new ArrayList<Menu>();
				Map<Long,List<Menu>> map = new HashMap<Long,List<Menu>>();
				for(Resource resource:userOperationList){
					if("1".equals(resource.getIsMenu())){
						Menu menu = new Menu();
						menu.setId(resource.getId());
						menu.setName(resource.getName());
						menu.setUrl(resource.getUrl());
						if(resource.getParentResource()!=null){
							userOperationURIMap.put(resource.getUrl(), resource.getUrl());
							//userOperationURIList.add(resource.getUrl());
							if(map.containsKey(resource.getParentResource().getId())){
								map.get(resource.getParentResource().getId()).add(menu);
							}else{
								List<Menu> list = new ArrayList<Menu>();
								list.add(menu);
								map.put(resource.getId(), list);
							}
						}else{
							List<Menu> list = new ArrayList<Menu>();
							map.put(resource.getId(), list);
							menus.add(menu);
						}
					}else{
						if(resource.getParentResource()!=null){
							userOperationURIMap.put(resource.getUrl(), resource.getUrl());
						}	
					}
				}
				for(Menu menu:menus){
					if(map.containsKey(menu.getId())){
						menu.setMenus(map.get(menu.getId()));
					}
				}
				session.setAttribute(ConstType.USER_MENUS, menus);
				session.setAttribute(ConstType.USER_OPERATION_URIS_MAP, userOperationURIMap);
				session.setAttribute(ConstType.ROLE_NEWS_COLUMN_IDS, roleColumnIdList);
			}
			return "redirect:/system/main";
		}else{
			//在session中设置用户名或密码错误标志
			session.setAttribute("userNameOrpasswordIsError", true);
			return "redirect:/system/login";
		}
	}
	/**
	 * 登录操作
	 * @param loginName
	 * @param loginPassword
	 * @return
	 */
	@RequestMapping(value="/main")
	public String index(HttpServletRequest request) {
		if(request.getSession().getAttribute(ConstType.ADMIN_SESSION_USER)!=null){
			return "system/index";
		}
		return "system/login";
	}
	/**
	 * 登录后的top页面
	 * @return
	 */
	@RequestMapping(value="/top")
	public String top() {
		return "system/top";
	}
	/**
	 * 登录后的leftMenu页面
	 * @return
	 */
	@RequestMapping(value="/menu")
	public String menu() {
		return "system/leftMenu";
	}
	/**
	 * 登录后的drag页面
	 * @return
	 */
	@RequestMapping(value="/drag")
	public String drag() {
		return "system/drag";
	}
	/**
	 * 退出操作
	 * @param status
	 * @return
	 */
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String logout(SessionStatus status) {
		//释放session信息
		status.setComplete();
		return "system/login";
	}
}
